#include "stm32f10x.h"
void delay_nms(u16 time);
void TIM3_TimeBaseConfiguration(u16 arr, u16 psc);
void RCC_Configuration(void);
void GPIO_Configuration(void);

TIM_OCInitTypeDef TIM_OCInitStructure;
u16 CCR1_Val = 333;
u16 CCR2_Val = 249;
u16 CCR3_Val = 166;
u16 CCR4_Val = 83;
u16 PrescalerValue = 0;

int main() {
    RCC_Configuration();
    GPIO_Configuration();


    /**
     * TIM3:产生4路PWM信号,每路具有不同占空比
     * TIM3CLK的频率设置为24Mhz, 对应的预分频系数公式为
     * Prescaler = (TIM3CLK / TIM3 counter clock)-1 = 72000000/24000000
     * 
     * TIM3 的工作频率为36Khz: fTIM3 = TIM counter clock/(ARR+1) = 24MHz / 666 = 36KHz
     * 
     * TIM3 Channel1 duty cycle = (TIM3_CCR1 / TIM3_ARR) * 100 = 50%
     * TIM3 Channel2 duty cycle = (TIM3_CCR2 / TIM3_ARR) * 100 = 37.5%
     * TIM3 Channel3 duty cycle = (TIM3_CCR3 / TIM3_ARR) * 100 = 25%
     * TIM3 Channel4 duty cycle = (TIM4_CCR3 / TIM3_ARR) * 100 = 12.5%
    */


    PrescalerValue = (u16)(SystemCoreClock / 24000000) - 1; // 分频数 3分频
    TIM3_TimeBaseConfiguration(666, PrescalerValue);

    // PWM Channel1
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    //比较输出使能
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    // 当计数器计数到这个值的时候,电平发生跳变
    TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
    //输出极性:TIM输出比较极性高, 当定时器计数值小于CCR1_Val 时为高电平
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OC1Init(TIM3, &TIM_OCInitStructure);
    TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);

    // PWM channel2
    TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
    TIM_OC2Init(TIM3, &TIM_OCInitStructure);
    TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);

    // PWM Channel3
    TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
    TIM_OC3Init(TIM3, &TIM_OCInitStructure);
    TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);

    // PWM Channel4
    TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
    TIM_OC4Init(TIM3, &TIM_OCInitStructure);
    TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);

    TIM_ARRPreloadConfig(TIM3, ENABLE);
    TIM_Cmd(TIM3, ENABLE);
    while (1);
}

void RCC_Configuration() {
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB| RCC_APB2Periph_AFIO,
                           ENABLE);
}

void GPIO_Configuration() {
    GPIO_InitTypeDef GPIO_InitStructure;

    // PA6 --- TIM3_CH1 PA7 ---- TIM3_CH2
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);


    // PB0 --- TIM3_CH3 PB1 ---- TIM3_CH4
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
}

void TIM3_TimeBaseConfiguration(u16 arr, u16 psc) {
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;

    TIM_TimeBaseInitStructure.TIM_Prescaler = psc;  // 预分频数
    TIM_TimeBaseInitStructure.TIM_Period =
        arr - 1;  // (自动重装载值(ARR)的计数值)
    TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);
}
